package com.hsc.www.dealLetter;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class Config {

    public static final String DOG_EXCHANGE = "dogExchange";
    public static final String DOG_QUEUE = "dogQueue";
    public static final String DOG_ROUTING_KEY = "dogRoutingKey";


    public static final String DEAD_LETTER_EXCHANGE = "deadLetterExchange";
    public static final String DEAD_LETTER_ROUTING_KEY = "deadLetterRoutingKey";
    public static final String DEAD_LETTER_QUEUE = "deadLetterQueue";


    @Bean(name = DOG_EXCHANGE)
    public DirectExchange exchange() {
        return new DirectExchange(DOG_EXCHANGE);
    }


    @Bean(name = DOG_QUEUE)
    public Queue queue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY);
        arguments.put("x-overflow","reject-publish");
        arguments.put("x-max-length",5);
        return new Queue(DOG_QUEUE, true, false, false, arguments);
    }


    @Bean
    Binding bindingExchangeMessage1(@Qualifier(DOG_QUEUE) Queue queue, @Qualifier("dogExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DOG_ROUTING_KEY);
    }


    @Bean(name = DEAD_LETTER_EXCHANGE)
    public DirectExchange deadExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }


    @Bean(name = DEAD_LETTER_QUEUE)
    public Queue deadQueue() {
        return new Queue(DEAD_LETTER_QUEUE);
    }

    @Bean
    Binding bindingExchangeMessage2(@Qualifier(DEAD_LETTER_QUEUE) Queue queue, @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_ROUTING_KEY);
    }

}